package com.neusoft.zygh.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import com.neusoft.zygh.model.Orders;
import com.neusoft.zygh.model.TongJiData;
import com.neusoft.zygh.service.OrdersService;  
import com.neusoft.zygh.utils.*;  


@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 新增
     */
    @PostMapping("/insert")
    public Result<String> insert(@RequestBody Orders orders){
        int res=ordersService.insert(orders);
        if(res==1){
            return ResultGenerator.genSuccessResult();
        }else{
        	return ResultGenerator.genFailResult(null);
        }
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{ids}")
    public Result<String> delete(@PathVariable("ids") String ids){
    	if(null==ids||ids.equals("")){
    		return ResultGenerator.genFailResult(null);
    	}
        String str[]=ids.split(",");
        for(String s:str){
            ordersService.deleteByPrimaryKey(Integer.valueOf(s));
        }
        return ResultGenerator.genSuccessResult();
    }
    
    /**
     * 更新
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody Orders orders){
    	int res=ordersService.updateByPrimaryKey(orders);
    	if(res==1){
    		return ResultGenerator.genSuccessResult();
    	}else{
    		return ResultGenerator.genFailResult(null);
    	}
    }
    
    /**
     * 主键查询
     */
    @GetMapping("/info/{id}")
    public Result<?> selectById(@PathVariable("id") int id){
    	Orders orders = ordersService.selectByPrimaryKey(id);
    	if(null==orders){
    		return ResultGenerator.genFailResult(null);
    	}else{
    		return ResultGenerator.genSuccessResult(orders);
    	}
    }
    
    /**
     * 条件和排序的分页查询
     */
    @PostMapping("/search")
    public Map<String,Object> search(
    		@RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            Orders orders){
    	//设置分页参数
    	Map<String, Object> map = new HashMap<String, Object>();
        if (page != null && rows != null) {
            PageBean pageBean = new PageBean(Integer.parseInt(page),
                    Integer.parseInt(rows));
            map.put("start", pageBean.getStart());
            map.put("size", pageBean.getPageSize());
        }
        List<Orders> list=ordersService.select(map);
        int total=ordersService.count(map);
        Map<String,Object> result=new HashMap<>();
        result.put("rows", list);
        result.put("total", total);
        result.put("status", 200);
        return result;
    }
    
    /**
     * 统计每天的预约订单
     */
    @GetMapping("/analysisEveryday")
    public List<TongJiData> analysisEveryday(){
    	return ordersService.analysisEveryday();
    }
    
    /**
     * 根据用户id查询预约订单
     */
    @GetMapping("/selectByUserid/{userid}")
    public List<Orders> selectByUserid(@PathVariable("userid")int userid){
    	return ordersService.selectByUserid(userid);
    }
}